import json

import requests

AppKey_send = ''
AppSecret_send = ''  # 企业机器人

AppKey_Inquire = ''
AppSecret_Inquire = ''  # 企业内部应用


# 获取企业内部应用访问凭证（Token）
def accessToken(AppKey, AppSecret):
    url = 'https://api.dingtalk.com/v1.0/oauth2/accessToken'
    headers = {
        'Host': 'api.dingtalk.com',
        "Content-Type": "application/json",
    }
    data = {
        "appKey": AppKey,
        "appSecret": AppSecret,
    }
    Response = requests.post(url=url, headers=headers, data=json.dumps(data)).json()
    return Response['accessToken']


tonken_send = accessToken(AppKey_send, AppSecret_send)  # 企业机器人token
tonken_Inquire = accessToken(AppKey_Inquire, AppSecret_Inquire)  # 企业内部应用tonken


# print(tonken_Inquire)


def send(userId,username):
    url = 'https://api.dingtalk.com//v1.0/robot/oToMessages/batchSend'
    headers = {
        'Host': 'api.dingtalk.com',
        'x-acs-dingtalk-access-token': tonken_send,
        'Content-Type': 'application/json',
    }
    data = {
        "robotCode": AppKey_send,
        "userIds": "['" + userId + "']",
        "msgKey": "sampleMarkdown",
        "msgParam":"{\"text\": \""+username+"，你好！查询到您当日的研发日志还未提交，请抓紧提交，谢谢！\",\"title\": \"研发日志提醒\"}",
    }
    Response = requests.post(url, headers=headers, data=json.dumps(data)).json()
